#!/usr/bin/env node

const https = require('https'),
      log   = console.log,
      url   = require('url');

var addr  = 'https://api.github.com/search/repositories?q=user:' + (process.argv[2] || 'wangding');
var options = url.parse(addr);

options.headers = {
  'User-Agent': 'curl/7.29.0'
};

https.get(options, (res) => {
  log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);

  log(res.heders);

  var result = '';

  res.on('data', (data) => {
    result += data.toString('utf8');
  });
      
  res.on('end', () => {

    //log(JSON.parse(result).items);
    var reps = JSON.parse(result);

    log('Total:', reps.items.length);
    log('==========================');
    for(var i=0; i<reps.items.length; i++) {
      log('%d\t%s', (i + 1), reps.items[i].name);
    }

    var rs=[], num = 1;
    var data = JSON.parse(result).items;
    //log(data);
    data.forEach((item) => {
      //num: num++
      //name: item.name
      rs.push({
        name:item.name,
        description:item.description
      });
    });
    console.table(rs);
  });
  //console.table(rs);
});

